﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mail;
using System.Web;
using System.Net.Mail;
using System.Web.UI.WebControls;

namespace BitPortal.Utils
{
    public static class EmailUtils
    {
        /// <summary>
        /// Send Email using System.Web.Mail namespace
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="attachFileVirtualPath"></param>
        /// <param name="smtpServer"></param>
        public static void SendEmail(string from, string to, string subject, string body,
            string[] attachFileVirtualPaths = null, string smtpServer = "localhost")
        {
            System.Web.Mail.MailMessage MyMail = new System.Web.Mail.MailMessage();
            MyMail.From = from;
            MyMail.To = to;
            MyMail.Subject = subject;
            MyMail.Body = body;
            MyMail.BodyEncoding = System.Text.Encoding.UTF8;
            MyMail.BodyFormat = System.Web.Mail.MailFormat.Html;
            if (attachFileVirtualPaths != null)
            {
                foreach (string str in attachFileVirtualPaths)
                {
                    MyMail.Attachments.Add(new System.Web.Mail.MailAttachment(HttpContext.Current.Server.MapPath(str)));
                }
            }
            System.Web.Mail.SmtpMail.SmtpServer = smtpServer;
            System.Web.Mail.SmtpMail.Send(MyMail);
        }

        /// <summary>
        /// Send Email using System.Net.Mail namespace
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="attachFileVirtualPath"></param>
        /// <param name="smtpServer"></param>
        public static void SendEmail(string from, string[] to, string subject, string body, string[] cc = null, string[] bcc = null,
            string[] attachFileVirtualPaths = null, SmtpClient smtpServer = null)
        {
            System.Net.Mail.MailMessage MyMail = new System.Net.Mail.MailMessage();
            MyMail.From = new MailAddress(from);
            foreach (string str in to)
            {
                MyMail.To.Add(new MailAddress(str));
            }
            MyMail.Subject = subject;
            MyMail.Body = body;
            MyMail.BodyEncoding = System.Text.Encoding.UTF8;
            MyMail.IsBodyHtml = true;
            if (cc != null)
            {
                foreach (string str in cc)
                {
                    MyMail.CC.Add(new MailAddress(str));
                }
            }

            if (attachFileVirtualPaths != null)
            {
                foreach (string str in attachFileVirtualPaths)
                {
                    MyMail.Attachments.Add(new Attachment(HttpContext.Current.Server.MapPath(str)));
                }
            }

            if (smtpServer == null)
            {
                smtpServer = new SmtpClient();
            }
            smtpServer.Send(MyMail);
        }

        /// <summary>
        /// Gui email voi dinh dang noi dung html
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="cc"></param>
        /// <param name="bcc"></param>
        /// <param name="attachFileVirtualPaths"></param>
        /// <param name="smtpServer"></param>
        public static void SendEmailHtml(string from, string[] to, string subject, string body, string[] cc = null, string[] bcc = null,
            string[] attachFileVirtualPaths = null, SmtpClient smtpServer = null)
        {
            System.Net.Mail.MailMessage MyMail = new System.Net.Mail.MailMessage();
            MyMail.From = new MailAddress(from);
            foreach (string str in to)
            {
                MyMail.To.Add(new MailAddress(str));
            }
            MyMail.Subject = subject;
            MyMail.BodyEncoding = System.Text.Encoding.UTF8;
            MyMail.IsBodyHtml = true;
            AlternateView v = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
            MyMail.AlternateViews.Add(v);
            if (cc != null)
            {
                foreach (string str in cc)
                {
                    MyMail.CC.Add(new MailAddress(str));
                }
            }

            if (attachFileVirtualPaths != null)
            {
                foreach (string str in attachFileVirtualPaths)
                {
                    MyMail.Attachments.Add(new Attachment(HttpContext.Current.Server.MapPath(str)));
                }
            }

            if (smtpServer == null)
            {
                smtpServer = new SmtpClient();
            }
            smtpServer.Send(MyMail);
        }

    }
}
